package com.ostrich.note.ui.note.views;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import com.ostrich.note.R;
import com.ostrich.note.base.BaseMvvmActivity;
import com.ostrich.note.databinding.ActivityNoteSearchBinding;
import com.ostrich.note.entity.BaseResponseEntity;
import com.ostrich.note.ui.note.adapter.NoteAdapter;
import com.ostrich.note.ui.note.entity.NoteEntity;
import com.ostrich.note.ui.note.viewmodel.NoteSearchViewModel;

import java.util.List;

/**
 * 笔记本搜索
 */
public class NoteSearchActivity extends BaseMvvmActivity<ActivityNoteSearchBinding, NoteSearchViewModel>{
    private NoteAdapter mNoteAdapter;
    @Override
    protected void iniView() {
        mNoteAdapter = new NoteAdapter();
        binding.rv.setAdapter(mNoteAdapter);
        binding.rv.setLayoutManager(new LinearLayoutManager(this));
        //点击跳转到笔记详情
        mNoteAdapter.setOnItemClickListener(new NoteAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putInt("note_type_id",mNoteAdapter.getNoteByPosition(position).getNotepad_type_id());
                startActivityBundle(NoteDetailActivity.class,bundle);
            }
        });

    }

    @Override
    public int getLayout() {
        return R.layout.activity_note_search;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {
        binding.clBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        binding.etInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                viewModel.notepadSearchList(s.toString()).observe(NoteSearchActivity.this, new Observer<BaseResponseEntity<List<NoteEntity>>>() {
                    @Override
                    public void onChanged(BaseResponseEntity<List<NoteEntity>> listBaseResponseEntity) {
                        List<NoteEntity> data = listBaseResponseEntity.getData();
                        mNoteAdapter.clearData();
                        mNoteAdapter.setData(data);
                    }
                });
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        binding.ivClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.etInput.setText("");
            }
        });
    }

    @Override
    public void createViewModel() {
        viewModel = new ViewModelProvider(this).get(NoteSearchViewModel.class);
    }
}